package com.jiandan.meizi;

import java.util.LinkedList;

import com.jiandan.bean.JanDanNew;
import com.jiandan.load.JanDanNewAdpter;
import com.jiandan.utils.HtmlParser;
import com.jiandan.utils.SimpleHttpClient;
import com.jiandan.utils.Tools;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * http://jandan.net/new
 * 
 * @author Mr.He
 * 
 */
public class JanDanNewActivity extends Activity implements Runnable {

    private Handler handler;
    private String url = "http://jandan.net/new";
    private ListView listview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jandan_new);
        listview = (ListView) findViewById(R.id.listview);
        handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {

                Tools.progressDialogDissmiss();
                
                if (msg.obj != null) {

                    listview.setAdapter(new JanDanNewAdpter(JanDanNewActivity.this,
                            (LinkedList<JanDanNew>) msg.obj));

                    listview.setOnItemClickListener(new OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

                            JanDanNew dan = (JanDanNew) listview.getItemAtPosition(position);
                            Intent in = new Intent();
                            in.setClass(JanDanNewActivity.this, JanDanNewDetailactivity.class);
                            in.putExtra("url", dan.getUrl());
                            startActivity(in);
                        }
                    });
                } else {

                    Toast.makeText(JanDanNewActivity.this, "获取失败，请重试", Toast.LENGTH_LONG).show();

                }

            }
        };

        Tools.buildLoadingDialog(this);
        new Thread(this).start();
    }

    @Override
    public void run() {
        Message msg = handler.obtainMessage();
        try {
            SimpleHttpClient client = new SimpleHttpClient();
            HtmlParser parser = new HtmlParser();
            parser.setHtml(client.get(url));
            parser.setActivity(JanDanNewActivity.this);
            msg.obj = parser.handleTitles();
            msg.sendToTarget();
        } catch (Exception e) {
            e.printStackTrace();
            msg.sendToTarget();
        }

    }



}
